package com.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.UserDao;
import com.domain.User;
import com.service.UserService;
import com.util.MD5Utils;


@Service(value="userService")
@Transactional
public class UserServiceImpl implements UserService{
	@Resource(name="userDao")
	private UserDao userDao;

	public User login(User model) {
		//将输入的明文的密码进行md5加密
		String password = MD5Utils.md5(model.getPassword());
		//String query ="from User where username=? and password = ?";
		List<User> list = userDao.findByNamedQuery("findByUsernameAndPassword", model.getUsername(),password);		//使用这种方式查询,必须在映射文件hbm.xml中邪query属性.
		//List<User> list = userDao.findUser(query, model.getUsername(), password);
		if(list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public void editPwd(User model) {			//修改密码
		String password = MD5Utils.md5(model.getPassword());
		System.out.println(password);
		
		model=userDao.findById(model.getId());
		model.setPassword(password);
		//userDao.update(model);			//持久态修改属性会默认发送sql,修改数据库.
	}
}
